package com.example.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.ResultData;
import com.example.order.entity.Order;
import com.example.order.entity.OrderQuery;
import com.example.order.entity.PayLog;
import com.example.order.service.OrderService;
import com.example.order.service.PayLogService;
import com.example.servicebase.exceptionhandler.CustomException;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-05
 */
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private PayLogService payLogService;

    //根据课程id和用户id查询订单，根据状态判断当前用户是否成功购买了当前课程
    @GetMapping(value = "isBuyCourse/{courseId}/{userId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public boolean isBuyCourse(@PathVariable String courseId ,@PathVariable String userId){
        QueryWrapper<Order> wrapper =new QueryWrapper<>();
        wrapper.eq("course_id",courseId)
                .eq("member_id",userId)
                .eq("status",1);
        long count = orderService.count(wrapper);
        if(count == 0){
            //表示没有当前用户没有购买当前课程
            return false;
        }
        return true;
    }

    //生成订单的方法
    @GetMapping(value = "createOrder/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData CreateOrder(@PathVariable String courseId, HttpServletRequest request){
        String token = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtil.isNullOrEmpty(token)) throw new CustomException(20001,"请先登录");
        String orderNo=orderService.createOrder(courseId, token);
        //返回订单号
        return ResultData.ok().data("orderId",orderNo);
    }

    //根据id查询订单
    @GetMapping(value = "getOrderById/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData GetOrderById(@PathVariable String id){
        QueryWrapper<Order> wrapper =new QueryWrapper<>();
        wrapper.eq("order_no",id);
        Order order = orderService.getOne(wrapper);
        return ResultData.ok().data("order",order);
    }
    @DeleteMapping(value = "delorder/{orderNo}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void DelOrder(@PathVariable String orderNo){
        QueryWrapper<PayLog> wrapper =new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        long count = payLogService.count(wrapper);
        if (count==0){
            //订单没有支付成功 在订单表中删除订单
            QueryWrapper<Order> wrapper1=new QueryWrapper<>();
            wrapper1.eq("order_no",orderNo);
            orderService.remove(wrapper1);
        }
    }
    //分页查询订单
    @PostMapping(value = "getPageOrder/{page}/{limit}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getPageOrder(@PathVariable long page,
                                   @PathVariable long limit,
                                   @RequestBody(required = false) OrderQuery orderQuery){
        Page<Order> pageOrder=new Page<>(page,limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //设置分页查询条件，按照时间排序
        wrapper.orderByDesc("gmt_create");
        String name = orderQuery.getName();
        Integer level = orderQuery.getLevel();
        String begin = orderQuery.getBegin();
        String end = orderQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("course_title",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("status",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        orderService.page(pageOrder, wrapper);
        //将查询到的数据封装到List中
        List<Order> list=pageOrder.getRecords();
        long total = pageOrder.getTotal();
        return ResultData.ok().data("total",total).data("rows",list);
    }

    //更新订单
    @PostMapping(value = "updateOrderInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData UpdateOrderInfo(@RequestBody Order order){
        orderService.updateById(order);
        return ResultData.ok();
    }

    @DeleteMapping(value = "deleteOrderById/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData DeleteOrderById(@PathVariable String id){
        orderService.removeById(id);
        return ResultData.ok();
    }
}

